<?php

/**
 * traitementMedicament actions.
 *
 * @package    app_clinique
 * @subpackage traitementMedicament
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class traitementMedicamentActions extends autotraitementMedicamentActions
{
    public function executeCreateTraitementMedicament(){

         $this->sf_traitement_medicament = $this->getsfTraitementMedicamentOrCreate();
         $this->idtraitement = $this->getRequestParameter('idtraitement');
         $this->sf_traitement = sfTraitementPeer::retrieveByPk($this->idtraitement);
         if ($this->getRequest()->getMethod() == sfRequest::POST){

                $this->updatesfTraitementMedicamentFromRequest();
                $this->sf_medicament = $this->sf_traitement_medicament->getsfMedicament();
                if(($this->sf_medicament->getQteEnStock() - $this->sf_traitement_medicament->getQuantite()) < 0){
                     $this->labels = $this->getLabels();
                     $this->redirect('traitementMedicament/createTraitementMedicament?erreur=2&idtraitement='.$this->sf_traitement_medicament->getTraitementId());
                }

                $this->savesfTraitementMedicament($this->sf_traitement_medicament);
                $this->setFlash('notice', 'Vos modifications ont été enregistrées');

                //update de la quantité médicament en stock
                $this->sf_medicament->setQteEnStock($this->sf_medicament->getQteEnStock() - $this->sf_traitement_medicament->getQuantite());
                $this->savesfMedicament($this->sf_medicament);

                return $this->redirect('traitementMedicament/show?idtraitement='.$this->sf_traitement_medicament->getTraitementId());

         }
         else
         {
              $this->labels = $this->getLabels();
         }
    }

    public function executeShow(){
       $this->idtraitement = $this->getRequestParameter('idtraitement');
       $this->sf_traitement = sfTraitementPeer::retrieveByPk($this->idtraitement);
       //liste des traitements pour la consultation transmise en parametre
       $c = new Criteria();
       $c->add(sfTraitementMedicamentPeer::TRAITEMENT_ID, $this->idtraitement);
       $this->listeTraitementMedicaments = sfTraitementMedicamentPeer::doselect($c);

    }

    public function executeQuantiteStock(){
       $this->idmedicament = $this->getRequestParameter('idmedicament');
       //$this->idmedicament = 1;
       //qte en stock pour le médicament transmis en parametre
       $c = new Criteria();
       $c->add(sfMedicamentPeer::ID, $this->idmedicament);
       $this->liste_medicaments = sfMedicamentPeer::doselect($c);
    }
    
    public function handleErrorCreateTraitementMedicament()
  {
    $this->preExecute();
    $this->sf_traitement_medicament = $this->getsfTraitementMedicamentOrCreate();
    $this->updatesfTraitementMedicamentFromRequest();

    $this->labels = $this->getLabels();
    $this->redirect('traitementMedicament/createTraitementMedicament?erreur=1&idtraitement='.$this->sf_traitement_medicament->getTraitementId());
  }

    public function executeEdit()
  {
    $this->sf_traitement_medicament = $this->getsfTraitementMedicamentOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updatesfTraitementMedicamentFromRequest();

      $this->savesfTraitementMedicament($this->sf_traitement_medicament);

      $this->setFlash('notice', "L'enregistrement a été effectué");

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('traitementMedicament/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('traitementMedicament/list');
      }
      else
      {
        return $this->redirect('traitementMedicament/edit?id='.$this->sf_traitement_medicament->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  protected function savesfMedicament($sf_medicament)
  {
    $sf_medicament->save();

  }
}
